package com.smartleanx.module.hrm.service.operationlog;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartleanx.framework.common.util.collection.CollectionUtils;
import com.smartleanx.module.hrm.framework.operate.core.annotations.OperationLog;
import com.smartleanx.module.hrm.intergration.AdminUserServiceProvider;
import com.smartleanx.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.regex.Pattern;

import com.smartleanx.module.hrm.controller.admin.operationlog.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.operationlog.OperationLogDO;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.operationlog.OperationLogMapper;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 操作日志记录 Service 实现类
 *
 * @author 精益大师
 */
@Service
@Validated
public class OperationLogServiceImpl implements OperationLogService {

    @Resource
    private OperationLogMapper operationLogMapper;
    @Resource
    private AdminUserServiceProvider adminUserServiceProvider;

    @Override
    @Async
    public Long createOperationLog(OperationLogSaveReqVO createReqVO) {
        // 插入
        OperationLogDO operationLog = BeanUtils.toBean(createReqVO, OperationLogDO.class);
        operationLogMapper.insert(operationLog);
        // 返回
        return operationLog.getId();
    }

    @Override
    public OperationLogDO getOperationLog(Long id) {
        return operationLogMapper.selectById(id);
    }

    @Override
    public PageResult<OperationLogRespVO> getOperateLogPage(OperationLogPageReqVO pageReqVO) {
        PageResult<OperationLogDO> list = operationLogMapper.selectPage(pageReqVO);
        PageResult<OperationLogRespVO> pageResult = BeanUtils.toBean(list, OperationLogRespVO.class);
         if (CollUtil.isNotEmpty(pageResult.getList())) {
            Set<Long> userIds = new HashSet<>();
            Pattern pattern = Pattern.compile("\\d+");
             pageResult.getList().forEach(logRespDTO -> {
                String operator = logRespDTO.getOperator();
                if (StrUtil.isNotBlank(operator) && pattern.matcher(operator).matches()) {
                    userIds.add(Long.valueOf(operator));
                }
            });
            Map<Long, AdminUserRespDTO> userMap = userIds.isEmpty() ? MapUtil.empty() : adminUserServiceProvider.getAllUserMap(userIds);
             pageResult.getList().forEach(logRespDTO -> {
                String operator = logRespDTO.getOperator();
                if (StrUtil.isNotBlank(operator) && pattern.matcher(operator).matches()) {
                    logRespDTO.setOperatorName(userMap.get(Long.valueOf(operator)).getNickname());
                }
            });
        }
        return pageResult;
    }

    @Override
    public void deleteByUserId(Long userId) {
        operationLogMapper.delete(new LambdaQueryWrapper<OperationLogDO>(){{
            eq(OperationLogDO::getOperator,userId);
        }});
    }

}
